var app = getApp(),
  $ = require("../../utils/httpHelper.js");
var address = require('../../utils/city.js')

Page({

  /**
   * 页面的初始数据
   * 当前    provinces:所有省份
   * citys选择省对应的所有市,
   * areas选择市对应的所有区
   * provinces：当前被选中的省
   * city当前被选中的市
   * areas当前被选中的区
   */
  data: {
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: '',
    sheng:'',
    shi:'',
    qu:'',
    moren:true,
    username:'',
    phone:'',
    addreinfo:'',
    areaInfo:'',
    edit:false,
    fromobl:false,
    fromobl2: false,
    das:'',
    address_id:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 初始化动画变量
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    this.animation = animation;
    // 默认联动显示北京
    var id = address.provinces[0].id
    this.setData({
      provinces: address.provinces,
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id],
    })
    
    if (!app.check_empty(options.address_id)){
      let params = {
        rd_session: app.globalData.userInfo.rd_session,
        address_id: options.address_id
      }
      $.httpPost("get_address_info", params, (data, err) => {
        this.setData({
          edit:true,
          username: data.name,
          phone:data.phone,
          addreinfo: data.detail_address,
          areaInfo: data.address,
          moren:data.is_default=='y'?true:false,
          address_id: options.address_id
        })
      })
    } else if (!app.check_empty(options.from) && options.from == 'obl'){
      
      this.setData({
        das: `?good_id=${options.good_id}&num=${options.num}&status=${options.status}&from=addr`,
        fromobl:true
      })
    } 
    
  },


 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name
    that.setData({
      areaInfo: areaInfo,
      sheng: that.data.provinces[value[0]].name,
      shi: that.data.citys[value[1]].name,
      qu: that.data.areas[value[2]].name
    })
  },
  
  // 处理省市县联动逻辑
  cityChange: function (e) {
    console.log(e)
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
    console.log(this.data)
  },

  switch1Change: function (e) {
    this.setData({
      moren:e.detail.value
    })
  },

  inputuser(e){
    this.setData({
      username: e.detail.value
    })
  },
  inputaddress(e){
    this.setData({
      addreinfo: e.detail.value
    })
  },
  inputphone(e){
    this.setData({
      phone: e.detail.value
    })
  },

  tj(){
    if (!app.check_empty(this.data.username) &&
      !app.check_empty(this.data.addreinfo) &&
      !app.check_empty(this.data.phone) &&
      !app.check_empty(this.data.areaInfo)){
      if (app.checkPhone(this.data.phone)){ 
        if (this.data.edit) {
          //编辑
          let param = {
            rd_session: app.globalData.userInfo.rd_session,
            name: this.data.username,
            phone: this.data.phone,
            address: this.data.areaInfo,
            detail_address: this.data.addreinfo,
            address_id: this.data.address_id,
            is_default:this.data.moren?"y":"n"
          }
          $.httpPost("edit_address", param, (data, err) => {
            if (data.result == 'success') {
              wx: wx.showToast({
                title: data.message,
                duration: 2000,
              })
              wx.redirectTo({
                url: '/pages/address/addressli',
              })
            }
          })
        } else {
          //新增
          let params = {
            rd_session: app.globalData.userInfo.rd_session,
            name: this.data.username,
            phone: this.data.phone,
            address: this.data.areaInfo,
            detail_address: this.data.addreinfo,
            is_default: this.data.moren ? "y" : "n"
          }
          $.httpPost("add_address", params, (data, err) => {
            if (data.result == 'success') {
              wx.showToast({
                title: data.message,
                duration: 2000,
              })
              if (this.data.fromobl) {
                //去待付款
                console.log(this.data.das)
                wx.redirectTo({
                  url: '/pages/obligation/obligation' + this.data.das + '&addr_id='+data.address_id,
                })
              } else {
                //去地址列表
                wx.redirectTo({
                  url: '/pages/address/addressli',
                })
              }
            }
          })
        }
      }else{
        wx: wx.showToast({
          title: '手机号格式错误',
          duration: 2000,
        })
      }
      
    }else{
      wx:wx.showToast({
        title: '不能为空哦',
        duration: 2000,
      })
    }
    
  }

})
